// pages/publish/publish.js
const {
  post
} = require('../../utils/http')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: "点击选择，要勾选哦~",
    latitude: "",
    longitude: "",
    type: 'buy',
    message: '',
    contact: "",
    issubmit: false,
    issuccess: null
  },

  chooseLocation() {
    wx.chooseLocation({
      complete: (res) => {
        console.log(res);
        this.setData({
          longitude: res.longitude,
          latitude: res.latitude,
          address: res.address
        })
      },
    })
  }
  //接口参数：address，latitude，longitude，message，contact，type（sell，buy），openid（可不传）
  ,
  typechange(event) {
    console.log(event)
    this.setData({
      type: event.detail.value
    })
  },
  messageinput(event) {
    this.setData({
      message: event.detail.value
    })
  },
  contactinput(event) {
    this.setData({
      contact: event.detail.value
    })
  },

  async handleSubmit() {

    let options = {
      mask: true,
      duration: 2000,
      icon: "none"
    }

    if (this.data.address === "点击选择，要勾选哦~") {
      wx.showToast({
        title: '地址不能为空',
        ...options
      })
      return;
    }

    if (!this.data.message) {
      wx.showToast({
        title: '说明不能为空',
        ...options
      })
      return;
    }

    if (!this.data.contact) {
      wx.showToast({
        title: '联系方式不能为空',
        ...options
      })
      return;
    }


    let params = {
      address: this.data.address,
      latitude: this.data.latitude,
      longitude: this.data.longitude,
      message: this.data.message,
      contact: this.data.contact,
      type: this.data.type
    }
    wx.showLoading({
      title: '',
    })
    let rs = await post('index.php/trade/add_item', params)
    wx.hideLoading()
    this.setData({
      issubmit: true,
      issuccess: rs.ret
    })
    console.log(rs);
  },

  goHomePage() {
    wx.navigateTo({
      url: '/pages/index/index',
    })
  },
  republish() {
    this.setData({
      issuccess: null,
      issubmit: false
    })
  }



})